package com.example.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.VO.EmpDepVO;
import com.example.entity.DepartmentsEntity;
import com.example.entity.Employees;
import com.example.entity.Jobs;
import com.example.entity.Locations;
import com.example.feign.DepFeignService;
import com.example.mapper.EmployeesMapper;
import com.example.mapper.JobsMapper;
import com.example.mapper.LocationsMapper;
import com.example.service.EmployeesService;
import com.example.utils.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class EmployeesServiceImpl implements EmployeesService {

    @Resource
    private EmployeesMapper employeesMapper;
    @Resource
    private DepFeignService depFeignService;
    @Resource
    private JobsMapper jobsMapper;
    @Resource
    private LocationsMapper locationsMapper;


    @Override
    public Object insert(Employees employees) {

        if (employees == null) {
            return ReturnT.error("参数缺失");
        }

        employeesMapper.insert(employees);
        return ReturnT.success();
    }


    @Override
    public Object delete(int id) {
        int ret = employeesMapper.deleteById(id);
        return ret>0?ReturnT.success():ReturnT.error("删除错误");
    }


    @Override
    public Object update(Employees employees) {
        int ret = employeesMapper.updateById(employees);
        return ret>0?ReturnT.success():ReturnT.error("插入错误");
    }


    @Override
    public Employees load(int id) {
        return employeesMapper.selectById(id);
    }


    @Override
    public Map<String,Object> pageList(int offset, int pagesize) {

        List<Employees> pageList = employeesMapper.pageList(offset, pagesize);
        int totalCount = employeesMapper.pageListCount(offset, pagesize);

        // result
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

        return result;
    }

    @Override
    public ReturnT<List<EmpDepVO>> selectAllEmp(String firstName, String lastName, String departmentCode) {
        //使用mybatisplus进行模糊查询
        QueryWrapper<Employees> qw = new QueryWrapper<>();
        if(firstName != null && !firstName.isEmpty()){
            qw.like("first_name",firstName);
        }
        if(lastName != null && !lastName.isEmpty()){
            qw.like("last_name",lastName);
        }
        List<Employees> list = employeesMapper.selectList(qw);
        //通过feign获取部门的数据
        ReturnT<List<DepartmentsEntity>> returnT = depFeignService.selectByDepeartmentCode(departmentCode);

        if(returnT.getCode() != 200){
            return ReturnT.error("部门不存在");
        }
        //创建EmpDepVOList
        List<EmpDepVO> empDepVOList = new ArrayList<>();
        List<DepartmentsEntity> departmentsEntities = returnT.getData();
        for (Employees employees : list) {
            for (DepartmentsEntity departmentsEntity : departmentsEntities) {
                //如果部门id相同就添加进去
                if(employees.getDepartmentId().equals(departmentsEntity.getDepartmentId())){
                    //填充员工的信息
                    EmpDepVO empDepVO = new EmpDepVO();
                    BeanUtils.copyProperties(employees,empDepVO);
                    //填充部门的信息
                    BeanUtils.copyProperties(departmentsEntity,empDepVO);
                    //填充岗位
                    Jobs jobs = jobsMapper.selectOne(new QueryWrapper<Jobs>().eq("employee_id", employees.getEmployeeId()));
                    empDepVO.setJobName(jobs.getJobName());
                    //填充工作地点
                    Locations locations = locationsMapper.selectById(departmentsEntity.getLocationId());
                    if(locations != null){
                        String detailAddress = locations.getProvince()+" "+ locations.getCity()+" " + locations.getAddress();
                        empDepVO.setDetailAddress(detailAddress);
                    }
                    empDepVOList.add(empDepVO);
                    break;
                }
            }
        }

        return ReturnT.success(empDepVOList);
    }
}
